package com.kmxd.ams.app.system.command;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kmxd.ams.client.system.ISysDictItemSvc;
import com.kmxd.ams.client.system.ISysDictSetSvc;
import com.kmxd.ams.client.system.ISysDictSvc;
import com.kmxd.ams.client.system.dto.SysCropAddDTO;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.system.entity.SysCrop;
import com.kmxd.ams.infra.system.entity.SysDict;
import com.kmxd.ams.infra.system.entity.SysDictItem;
import com.kmxd.ams.infra.system.entity.SysDictSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

/**
 * 新建单位复制字典信息
 *
 * @author ZuoLG
 */
@Slf4j
@Service
@AllArgsConstructor
public class CopyDictCmd {
  private ISysDictSvc sysDictSvc;
  private ISysDictItemSvc sysDictItemSvc;
  private ISysDictSetSvc sysDictSetSvc;

  /**
   * 复制字典信息
   *
   * @param entity
   * @param req
   * @return
   */
  public boolean copyDict(SysCrop entity, SysCropAddDTO req) {
    // 1、查询字典,查询
    LambdaQueryWrapper<SysDict> queryWrapper = Wrappers.lambdaQuery();
    queryWrapper.eq(
        SysDict::getComId,
        ObjectUtil.isNotEmpty(req.getParentId()) ? req.getParentId() : SecurityUtil.getCropId());
    List<SysDict> dicts = sysDictSvc.list(queryWrapper);
    // 查询字典项
    Set<Long> dictIds = dicts.stream().map(SysDict::getId).collect(Collectors.toSet());

    LambdaQueryWrapper<SysDictItem> itemLambdaQueryWrapper = Wrappers.lambdaQuery();
    itemLambdaQueryWrapper.in(SysDictItem::getDictId, dictIds);
    List<SysDictItem> dictItems = sysDictItemSvc.list(itemLambdaQueryWrapper);
    // 查询字典系列

    LambdaQueryWrapper<SysDictSet> dictSetWrapper = Wrappers.lambdaQuery();
    dictSetWrapper.in(SysDictSet::getDictId, dictIds);
    List<SysDictSet> dictSets = sysDictSetSvc.list(dictSetWrapper);

    List<SysDictSet> dictSetsSave = Lists.newArrayList();
    // 更改字典对象
    List<SysDict> dictNew =
        dicts.stream()
            .map(
                dict -> {
                  Long oldId = dict.getId();
                  dict.setComId(entity.getId());
                  dict.setId(IdWorker.getId());
                  // 更改字典项
                  dictItems.stream()
                      .filter(dictItem -> dictItem.getDictId().equals(oldId))
                      .forEach(
                          item -> {
                            item.setId(IdWorker.getId());
                            item.setDictId(dict.getId());
                            if (StrUtil.isNotBlank(item.getSetCode())) {
                              // 更改多系列对象
                              dictSetsSave.addAll(
                                  dictSets.stream()
                                      .filter(
                                          sysDictSet ->
                                              sysDictSet.getCode().equals(item.getSetCode())
                                                  && sysDictSet.getDictId().equals(oldId))
                                      .map(
                                          dictSet -> {
                                            dictSet.setId(IdWorker.getId());
                                            dictSet.setDictId(dict.getId());
                                            return dictSet;
                                          })
                                      .collect(Collectors.toList()));
                            }
                          });

                  return dict;
                })
            .collect(Collectors.toList());
    sysDictSvc.saveBatch(dictNew);
    sysDictItemSvc.saveBatch(dictItems);
    sysDictSetSvc.saveBatch(dictSetsSave);
    sysDictSvc.clearAllCache();
    return true;
  }
}
